<?php

class PositionTable extends Doctrine_Table
{
    public function positionForDate($employeeId, DateTime $date){
        $q = $this->createQuery('p')
                ->where('p.employee_id = ?', $employeeId);
        $q->andWhere('(p.startDate <= ? AND p.endDate IS NULL) OR (p.startDate <= ? AND p.endDate >= ?)',
                array($date->format('y-m-d'),$date->format('y-m-d'), $date->format('y-m-d')));
        $positions = $q->execute();
        //echo "JP: " . count($positions) ."\n";
         if(count($positions)>1){
             //TODO Mejorar el mensaje de error incluyendo $employeeId y $date
             throw new Exception("More than onw position for employee XX on date XXXX");
             //throw new Exception(sprintf( "More than onw position for employee %s on date %s", $employeeId, $date->format('y-m-d'));
         }
        return $positions[0];
    }
}
